<?php

/*
 * Copyright © 2010 - 2012 Modo Labs Inc. All rights reserved.
 *
 * The license governing the contents of this file is located in the LICENSE
 * file located at the root directory of this distribution. If the LICENSE file
 * is missing, please contact sales@modolabs.com.
 *
 */

/**
  * @package Module
  * @subpackage Home
  */

/**
  * @package Module
  * @subpackage Home
  */

class HomeWebModule extends WebModule {
    protected $id = 'home';
    protected $canBeAddedToHomeScreen = false;
    protected $hideFooterLinks = true;
    
    protected function showLogin() {
        return $this->getOptionalModuleVar('SHOW_LOGIN', true);
    }
    
    private function getTabletModulePanes($tabletConfig) {
        $modulePanes = array();
        
        foreach ($tabletConfig as $blockName => $moduleID) {
            $title = ucfirst($moduleID);
            
            try {
                $module = self::factory($moduleID, 'pane', array());
                
                // Allow module to add javascript and css urls
                $module->fetchPage();
                
                // don't copy inline css and javascript which will be added on  
                // pane content ajax load. See common/templates/pane.tpl
                $cssAndJavascript = $module->exportCSSAndJavascript();
                $cssAndJavascript['properties']['inlineCSSBlocks'] = array();
                $cssAndJavascript['properties']['inlineJavascriptBlocks'] = array();
                $cssAndJavascript['properties']['inlineJavascriptFooterBlocks'] = array();
                $cssAndJavascript['properties']['onOrientationChangeBlocks'] = array();
                $cssAndJavascript['properties']['onLoadBlocks'] = array();
                $this->importCSSAndJavascript($cssAndJavascript);

                $pageData = $module->getPageData();
                if (isset($pageData['pane'], 
                          $pageData['pane']['pageTitle']) && strlen($pageData['pane']['pageTitle'])) {
                    
                    $title = $pageData['pane']['pageTitle'];
                } elseif (isset($pageData[$this->page], 
                          $pageData[$this->page]['pageTitle']) && strlen($pageData[$this->page]['pageTitle'])) {
                    
                    $title = $pageData[$this->page]['pageTitle'];
                }
                
            } catch (Exception $e) {
                Kurogo::log(LOG_WARNING, $e->getMessage(), "home", $e->getTrace());
            }
            
            $modulePanes[$blockName] = array(
                'id'        => $moduleID,
                'url'       => self::buildURLForModule($moduleID, 'index'),
                'title'     => $title,
                'elementId' => "content_{$moduleID}",
                'ajaxURL'   => FULL_URL_PREFIX.ltrim($this->buildURL('pane', array('id' => $moduleID)), '/'),
            );
        }
        return $modulePanes;
    }
    
    protected function runFederatedSearchForModule($module, $searchTerms) {
        $results = array();
        try {
            $total = $module->federatedSearch($searchTerms, 2, $results);
        } catch (Exception $e) {
            $total = 0;
            Kurogo::log(LOG_WARNING, 'Federated search for module '.$module->getID().' failed: '.
                $e->getMessage(), 'module');
        }
        return array(
            'items' => $results,
            'total' => $total,
            'url'   => $module->urlForFederatedSearch($searchTerms),
        );
    }
    
    public function getAvailableAlertModules() {
        $allModules = $this->getAllModules();
        $modules = array();
        foreach ($allModules as $module) {
            if ($module instanceOf HomeAlertInterface) {
                $modules[$module->getConfigModule()] = sprintf("%s (%s)", $module->getModuleName(), $module->getConfigModule());
            }
        }
        return $modules;
    }
    
    protected function initializeForPage() {
        switch ($this->page) {
            case 'help':
                break;
                  
            case 'index':
                $this->redirectToModule('notes', 'index');
                break;
          
            case 'searchResult':
                $moduleID = $this->getArg('id');
                $searchTerms = $this->getArg('filter');
                
                $module = self::factory($moduleID);
                $this->assign('federatedSearchResults', $this->runFederatedSearchForModule($module, $searchTerms));
                break;
                
            case 'pane':
                // This wrapper exists so we can catch module errors and prevent redirection to the error page
                $moduleID = $this->getArg('id');
                
                try {
                    $module = self::factory($moduleID, 'pane', array(self::AJAX_PARAMETER => 1));
                    $content = $module->fetchPage();
                    
                } catch (Exception $e) {
                    Kurogo::log(LOG_WARNING, $e->getMessage(), "home", $e->getTrace());
                    $content = '<p class="nonfocal">'.$this->getLocalizedString('ERROR_MODULE_PANE').'</p>';
                }
                
                $this->assign('content', $content);
                break;
        }
    }
}
